package com.fnzx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fnzx.entity.dto.ProjectDto;
import com.fnzx.entity.dto.ScoreDto;
import com.fnzx.entity.dto.User;
import com.fnzx.entity.vo.PersonalProjectVo;
import com.fnzx.entity.vo.ProjectAdminVo;
import com.fnzx.entity.vo.ProjectScoreAdminVo;
import com.fnzx.entity.vo.ProjectVo;
import com.fnzx.service.ProjectService;
import com.fnzx.service.ScoreService;
import com.fnzx.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "项目模块")
@RequestMapping("iot/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @Autowired
    private ScoreService scoreService;

    @ApiOperation(value = "新建项目",notes = "{\n" +
            "\t\"categoryId\": 5,\n" +
            "\t\"contact\": \"cheerful@qq.com\",\n" +
            "\t\"createdAdminId\": 1,\n" +
            "\t\"demandId\": 1454738066819297282,\n" +
            "\t\"projectName\": \"iot官网\",\n" +
            "\t\"userRelations\": [\n" +
            "\t\t{\n" +
            "\t\t\t\"cooperativeId\": 2,\n" +
            "\t\t\t\"personalId\": 1454737599594803201,\n" +
            "\t\t\t\"position\": \"前端工程师\"\n" +
            "\t\t},\n" +
            "\t\t{\n" +
            "\t\t\t\"cooperativeId\": 2,\n" +
            "\t\t\t\"personalId\": 1454737645417574402,\n" +
            "\t\t\t\"position\": \"前端工程师\"\n" +
            "\t\t},\n" +
            "\t\t{\n" +
            "\t\t\t\"cooperativeId\": 2,\n" +
            "\t\t\t\"personalId\": 1454737672206594049,\n" +
            "\t\t\t\"position\": \"后端工程师\"\n" +
            "\t\t},\n" +
            "\t\t{\n" +
            "\t\t\t\"cooperativeId\": 2,\n" +
            "\t\t\t\"personalId\": 1454737672206594049,\n" +
            "\t\t\t\"position\": \"ui工程师\"\n" +
            "\t\t}\n" +
            "\t]\n" +
            "}")
    @PostMapping("/add")
    public R buildProject(@RequestBody ProjectDto projectDto){
        projectService.buildProject(projectDto);
        return R.ok();
    }

    @ApiOperation(value = "查询项目",notes = "查询项目")
    @GetMapping("/list")
    public R listProject(){
        List<ProjectAdminVo> projectVoList = projectService.listProject();
        List<ProjectAdminVo> newProjectVoList = new ArrayList<>();
        for (ProjectAdminVo projectVo : projectVoList) {
            if (projectVo.getProjectName() != null) {
                newProjectVoList.add(projectVo);
            }
        }
        return R.ok().put("projectVo",newProjectVoList);
    }

    @ApiOperation(value = "根据条件查询项目",notes = "根据条件查询项目status=0,date=2021-11-01")
    @GetMapping("/listByStatusAndDate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status",value = "状态",paramType = "query",required = false,dataType = "String"),
            @ApiImplicitParam(name = "date",value = "发布日期",paramType = "query",required = false,dataType = "String")
    })
    public R listProjectByStatus(@RequestParam(value = "status", required = false) String status, @RequestParam(value = "date", required = false) String date){
        List<ProjectVo> projectVoList = projectService.listProjectByStatus(status, date);
        List<ProjectVo> newProjectVoList = new ArrayList<>();
        for (ProjectVo projectVo : projectVoList) {
            if (projectVo.getProjectName() != null) {
                newProjectVoList.add(projectVo);
            }
        }
        return R.ok().put("projectVo",newProjectVoList);
    }

    @ApiOperation(value = "个人顾问用户的项目和评分",notes = "个人顾问用户的项目和评分")
    @GetMapping("/listByPersonalId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "personalId",value = "个人顾问id",paramType = "query",required = true,dataType = "String")
    })
    public R listProjectByStatus(@RequestParam(value = "personalId") String personalId){
        List<PersonalProjectVo> personalProjectVoList = projectService.listByPersonalId(personalId);
        for (PersonalProjectVo personalProjectVo : personalProjectVoList) {
            String adminScore = projectService.listPersonalScore(personalProjectVo.getProjectId(), "9");
            String enterpriseScore = projectService.listPersonalScore(personalProjectVo.getProjectId(),"11");
            JSONObject jsonObject = JSONObject.parseObject(adminScore);
            ScoreDto score = JSON.toJavaObject(jsonObject, ScoreDto.class);
            if (score != null) {
                List<User> users = score.getUsers();
                for (User user : users) {
                    if (personalId.equals(user.getPersonalId())) {
                        personalProjectVo.setAdminScore(Integer.parseInt(user.getScore()));
                    }
                }
            }
            JSONObject jsonObject1 = JSONObject.parseObject(enterpriseScore);
            ScoreDto score1 = JSON.toJavaObject(jsonObject1, ScoreDto.class);
            if (score1 != null) {
                List<User> users1 = score1.getUsers();
                for (User user : users1) {
                    if (personalId.equals(user.getPersonalId())) {
                        personalProjectVo.setEnterpriseScore(Integer.parseInt(user.getScore()));
                    }
                }
            }
        }
        List<PersonalProjectVo> newPersonalProjectVoList = new ArrayList<>();
        for (PersonalProjectVo personalProjectVo : personalProjectVoList) {
            if (personalProjectVo.getProjectName() != null) {
                newPersonalProjectVoList.add(personalProjectVo);
            }
        }
        return R.ok().put("PersonalProjectVoList",newPersonalProjectVoList);
    }


    /**
     * 删除
     */
    @ApiOperation(value = "删除项目", notes = "删除项目")
    @PostMapping("/delete")
    public R delete(@RequestParam("id") Long projectId,@RequestHeader String token){
        projectService.delete(projectId);
        return R.ok();
    }

    /**
     * 根据id查询项目
     */
    @ApiOperation(value = "根据id查询项目", notes = "根据id查询项目")
    @PostMapping("/listByCreateId")
    public R listByCreateId(@RequestParam("id") Long adminId,@RequestHeader String token){
        List<ProjectAdminVo> projectVoList = projectService.listProjectByAdmin(adminId);
        List<ProjectAdminVo> newProjectVoList = new ArrayList<>();
        for (ProjectAdminVo projectAdminVoVo : projectVoList) {
            if (projectAdminVoVo.getProjectName() != null) {
                newProjectVoList.add(projectAdminVoVo);
            }
        }
        return R.ok().put("projectVo",newProjectVoList);
    }


    @ApiOperation(value = "根据条件查询个人项目",notes = "根据条件查询项目status=0,date=2021-11-01")
    @GetMapping("/listByStatusAndDateAndId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status",value = "状态",paramType = "query",required = false,dataType = "String"),
            @ApiImplicitParam(name = "date",value = "发布日期",paramType = "query",required = false,dataType = "String"),
            @ApiImplicitParam(name = "id",value = "id",paramType = "query",required = false,dataType = "String")
    })
    public R listProjectByStatusAndId(@RequestParam(value = "status", required = false) String status, @RequestParam(value = "date", required = false) String date, @RequestParam(value = "id", required = false) String adminId){
        List<ProjectScoreAdminVo> projectVoList = projectService.listProjectByStatusAndId(status, date, adminId);
        for (ProjectScoreAdminVo projectAdminVo : projectVoList) {
            String adminProjectScore = projectService.listPersonalScore(projectAdminVo.getProjectId(), "9");
            String enterpriseProjectScore = projectService.listPersonalScore(projectAdminVo.getProjectId(),"11");
            JSONObject jsonObject = JSONObject.parseObject(adminProjectScore);
            ScoreDto score = JSON.toJavaObject(jsonObject, ScoreDto.class);
            if (score != null) {
                if (score.getProject() != null) {
                    projectAdminVo.setAdminProjectScore(Integer.parseInt(score.getProject().getProjectScore()));
                }
            }
            JSONObject jsonObject1 = JSONObject.parseObject(enterpriseProjectScore);
            ScoreDto score1 = JSON.toJavaObject(jsonObject1, ScoreDto.class);
            if (score1 != null) {
                if (score1.getProject() != null) {
                    projectAdminVo.setEnterpriseProjectScore(Integer.parseInt(score1.getProject().getProjectScore()));
                }
            }
        }
        System.out.println(projectVoList);
        List<ProjectScoreAdminVo> newProjectVoList = new ArrayList<>();
        for (ProjectScoreAdminVo projectVo : projectVoList) {
            if (projectVo.getProjectName() != null) {
                newProjectVoList.add(projectVo);
            }
        }

        return R.ok().put("projectVo",newProjectVoList);
    }


    /**
     * 删除
     */
    @ApiOperation(value = "修改项目状态", notes = "修改项目状态")
    @PostMapping("/updateStatus")
    public R updateStatus(@RequestParam("id") String projectId, @RequestParam("status") String status){
        projectService.updateStatus(projectId, status);
        return R.ok();
    }


}